package com.yayan.xh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.yayan.xh.bean.GradeBean;

public class GradeDao extends ConnectionBase implements IGradeDao {
	
	private Connection conn = null;
	
	private QueryRunner qRunner = null;
	
	private PreparedStatement ps = null;
	
	private ResultSet rsKey = null;
	
	@Override
	public List<GradeBean> getGradeList(Map<String, String> map) throws Exception {
		List<GradeBean> result = null;
		try {
			qRunner = new QueryRunner();
			String sql = "select * from tb_grade where shop_id=?";
			conn = getConn();
			result =  qRunner.query(conn, sql, new BeanListHandler<GradeBean>(GradeBean.class), map.get("shopId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public GradeBean getGradeById(Map<String, String> map) throws Exception {
		GradeBean result = null;
		try {
			qRunner = new QueryRunner();
			String sql = "select * from tb_grade where shop_id=? and g_id=?";
			conn = getConn();
			result =  qRunner.query(conn, sql, new BeanHandler<GradeBean>(GradeBean.class), map.get("shopId"), map.get("gradeId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public boolean saveGrade(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "insert into tb_grade(shop_id, g_integral, g_grade, g_sale) values(?,?,?,?)";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, map.get("integral"));
			ps.setObject(3, map.get("grade"));
			ps.setObject(4, map.get("sale"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public boolean modifyGrade(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "update tb_grade set g_integral=?, g_grade=?, g_sale=? where shop_id=? and g_id=?";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("integral"));
			ps.setObject(2, map.get("grade"));
			ps.setObject(3, map.get("sale"));
			ps.setObject(4, map.get("shopId"));
			ps.setObject(5, map.get("gradeId"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public boolean delGrade(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "delete from tb_grade where shop_id=? and g_id=?";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, map.get("gradeId"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

}
